package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.entity.SysCourseLocation;
import com.markerhub.entity.SysLocation;
import com.markerhub.mapper.SysCourseLocationMapper;
import com.markerhub.service.SysCourseLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.service.SysLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-03-31
 */
@Service
public class SysCourseLocationServiceImpl extends ServiceImpl<SysCourseLocationMapper, SysCourseLocation> implements SysCourseLocationService {

    @Autowired
    SysLocationService sysLocationService;


    @Override
    public SysCourseLocation getByCourse(Long id) {
        return getOne(new QueryWrapper<SysCourseLocation>().eq("course_id", id));
    }

    @Override
    public Long getLocationByCourse(Long id) {
        return getOne(new QueryWrapper<SysCourseLocation>().eq("course_id", id)).getLocationId();
    }

    @Override
    public SysCourseLocation getByLocationId(Long id) {
        return getOne(new QueryWrapper<SysCourseLocation>().eq("location_id", id));
    }

    @Override
    public SysLocation info(Long courseId){

        SysCourseLocation sysCourseLocation= this.getOne(new QueryWrapper<SysCourseLocation>().eq("course_id",courseId));

        if(sysCourseLocation==null){return null;}

        SysLocation sysLocation = sysLocationService.getOne(new QueryWrapper<SysLocation>().eq("id",sysCourseLocation.getLocationId()));

        return sysLocation;

    }

    @Override
    public Boolean saveLocation(Long courseId,SysLocation sysLocation) {
        if(sysLocation==null){
            return false;
        }
        SysCourseLocation sysCourseLocation = new SysCourseLocation();
        sysCourseLocation.setLocationId(sysLocation.getId());
        sysCourseLocation.setCourseId(courseId);

        if (!this.save(sysCourseLocation)) {
            return false;
        } else {
            return true;
        }
    }

}
